﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace LabLibary.ExtractData
{
    public class Cybow300
    {
        public static List<ObjResults> ExtactData(string fileName)
        {
            string _SID = string.Empty;
            DateTime _Day = DateTime.Now;

            List<ObjResults> _lstResults = new List<ObjResults>();
            ObjResults _objResults = new ObjResults();

            string result = string.Empty;

            Dictionary<string, int> _dcMonth = new Dictionary<string,int>();
            _dcMonth.Add("JAN", 1);
            _dcMonth.Add("FEB", 2);
            _dcMonth.Add("MAR", 3);
            _dcMonth.Add("APR", 4);
            _dcMonth.Add("MAY", 5);
            _dcMonth.Add("JUN", 6);
            _dcMonth.Add("JUL", 7);
            _dcMonth.Add("AUG", 8);
            _dcMonth.Add("SEP", 9);
            _dcMonth.Add("OCT", 10);
            _dcMonth.Add("NOV", 11);
            _dcMonth.Add("DEC", 12);

            string _May = "1";
            string _GiaTri = string.Empty;
            DateTime _TGTra = DateTime.Now;

            try
            {
                using (StreamReader sr = new StreamReader(fileName))
                {
                    string _strAllData = sr.ReadToEnd();
                    string[] _strAllDatas = Regex.Split(_strAllData, "ID");
                    _strAllDatas = _strAllDatas[1].Split(')');

                    _SID = _strAllDatas[0].Replace("ID", string.Empty).Replace("(", string.Empty).Replace(" ", string.Empty);
                }
                    
                using (StreamReader sr = new StreamReader(fileName))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        try
                        {
                            if (line[0].ToString() == "~")
                            {
                                string[] _tmpStrArray = line.Split('/');
                                string _ngay = _tmpStrArray[1];
                                string _nam = _tmpStrArray[2].Substring(0, 4);
                                int _thang;
                                try
                                {
                                    _thang = _dcMonth[_tmpStrArray[0].Replace("~", string.Empty)];
                                }
                                catch { _thang = 1; }

                                _Day = new DateTime(int.Parse(_nam), _thang, int.Parse(_ngay), 0, 0, 0);
                            }

                            else if (line.Substring(0, 3).Trim() == "URO")
                            {
                                string tmp = line.Replace("URO", string.Empty).Trim();
                                _GiaTri = tmp.Substring(0, 5);
                                _lstResults.Add(new ObjResults(_Day, _SID, _May, "URO", _GiaTri, _TGTra));
                            }

                            else if (line.Substring(0, 3).Trim() == "GLU")
                            {
                                string tmp = line.Replace("GLU", string.Empty).Trim();
                                _GiaTri = tmp.Substring(0, 5);
                                _lstResults.Add(new ObjResults(_Day, _SID, _May, "GLU", _GiaTri, _TGTra));
                            }

                            else if (line.Substring(0, 3).Trim() == "BIL")
                            {
                                string tmp = line.Replace("BIL", string.Empty).Trim();
                                _GiaTri = tmp.Substring(0, 5);
                                _lstResults.Add(new ObjResults(_Day, _SID, _May, "BIL", _GiaTri, _TGTra));
                            }

                            else if (line.Substring(0, 3).Trim() == "KET")
                            {
                                string tmp = line.Replace("KET", string.Empty).Trim();
                                _GiaTri = tmp.Substring(0, 5);
                                _lstResults.Add(new ObjResults(_Day, _SID, _May, "KET", _GiaTri, _TGTra));
                            }

                            else if (line.Substring(0, 3).Trim() == "S.G")
                            {
                                _GiaTri = line.Replace("S.G", string.Empty).Trim();
                                _lstResults.Add(new ObjResults(_Day, _SID, _May, "S.G", _GiaTri, _TGTra));
                            }

                            else if (line.Substring(0, 3).Trim() == "BLD")
                            {
                                string tmp = line.Replace("BLD", string.Empty).Trim();
                                _GiaTri = tmp.Substring(0, 5);
                                _lstResults.Add(new ObjResults(_Day, _SID, _May, "BLD", _GiaTri, _TGTra));
                            }

                            else if (line.Substring(0, 2).Trim() == "pH")
                            {
                                _GiaTri = line.Replace("pH", string.Empty).Trim();
                                _lstResults.Add(new ObjResults(_Day, _SID, _May, "pH", _GiaTri, _TGTra));
                            }

                            else if (line.Substring(0, 3).Trim() == "PRO")
                            {
                                string tmp = line.Replace("PRO", string.Empty).Trim();
                                _GiaTri = tmp.Substring(0, 5);
                                _lstResults.Add(new ObjResults(_Day, _SID, _May, "PRO", _GiaTri, _TGTra));
                            }

                            else if (line.Substring(0, 3).Trim() == "NIT")
                            {
                                string tmp = line.Replace("NIT", string.Empty).Trim();
                                _GiaTri = tmp.Substring(0, 5);
                                _lstResults.Add(new ObjResults(_Day, _SID, _May, "NIT", _GiaTri, _TGTra));
                            }

                            else if (line.Substring(0, 3).Trim() == "LEU")
                            {
                                string tmp = line.Replace("LEU", string.Empty).Trim();
                                _GiaTri = tmp.Substring(0, 5);
                                _lstResults.Add(new ObjResults(_Day, _SID, _May, "LEU", _GiaTri, _TGTra));
                            }

                            else if (line.Substring(0, 3).Trim() == "VTC")
                            {
                                string tmp = line.Replace("VTC", string.Empty).Trim();
                                _GiaTri = tmp.Substring(0, 5);
                                _lstResults.Add(new ObjResults(_Day, _SID, _May, "VTC", _GiaTri, _TGTra));
                            }
                            
                        }
                        catch { }
                    }

                    return _lstResults;
                }
            }
            catch { }


            return null;
        }
    }
}
